package com.bjpowernode.money.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.model.BidInfo;
import com.bjpowernode.money.model.LoanInfo;
import com.bjpowernode.money.model.PageModel;
import com.bjpowernode.money.service.BidInfoService;
import com.bjpowernode.money.service.LoanInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author ZHAOZHENYU
 * @since 2022/10/15
 * 金融产品控制器
 */
@Controller
public class LoanController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", timeout = 20000)
    private LoanInfoService loanInfoService;

    @Reference(interfaceClass = BidInfoService.class, version = "1.0.0", timeout = 20000)
    private BidInfoService bidInfoService;

    /**
     * 1、投资产品列表页  并加入排行榜功能
     * @param pType
     * @param currentPage
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/loan/loan")
    public String showLoanPage(@RequestParam(name = "pType",required = true) Integer pType,Long currentPage,
                               Model model, HttpServletRequest request){

        PageModel pageModel = (PageModel)request.getSession().getAttribute("pageModel");
        if (pageModel == null) {
            pageModel = new PageModel(9);
            currentPage = pageModel.getFirstPage().longValue();
            request.getSession().setAttribute("pageModel",pageModel);
        }

        // 查出总条数并放进pageModel
        Long total = loanInfoService.queryLoanCountByType(pType);
        pageModel.setTotal(total);

        // 校验数据
        if(currentPage == null || currentPage <= 0) currentPage = pageModel.getFirstPage().longValue();
        Long lastPage = pageModel.getLastPage();
        if(currentPage  > lastPage) currentPage = lastPage;
        // 赋值当前页 不然空指针
        pageModel.setCurrentPage(currentPage);

        // TODO：逻辑分页

        List<LoanInfo> loanInfoList = loanInfoService.queryLoanProductPage(pType,pageModel);

        model.addAttribute("pType",pType);
        model.addAttribute("loanInfoList", loanInfoList);

        // redis中排行榜取出来 取出的数据order by bidMoney
        List<BidInfo> bidInfoList = loanInfoService.queryBidRankList();
        model.addAttribute("bidInfoList", bidInfoList);

        return "loan";

    }

    // TODO： 后端计算分页doFirst doPre doNext doLast

    /**
     * 2、查询选定投资产品的详细信息
     * @param loanId
     * @param model
     * @return
     */
    @GetMapping("/loan/loanInfo")
    public String showLoanInfo(@RequestParam(name = "loanId",required = true) Integer loanId,
                               Model model){

        LoanInfo loanInfo = loanInfoService.queryLoanInfoById(loanId);
        model.addAttribute("loanInfo", loanInfo);

        // 改为异步分页
        /*List<BidInfo> bidInfoList = bidInfoService.queryBidInfoById(loanId);
        model.addAttribute("bidInfoList", bidInfoList);*/

        return "loanInfo.html";
    }

    /**
     * 3、查出选定投资产品的所有投标信息【含有用户信息】 异步和分页
     * @param currentPage
     * @return
     */
    @GetMapping("/loan/BidInfoPage")
    @ResponseBody
    public Map BidInfoPage(@RequestParam(name = "currentPage",required = true) Long currentPage,
                           @RequestParam(name = "loanId",required = true) Integer loanId){
        PageModel pageModel = new PageModel(10);

        // 根据loanId竞标投资总数
        Long bidInfoCount = bidInfoService.queryBidInfoCount(loanId);
        pageModel.setTotal(bidInfoCount);

        // 校验当前页是否合理
        if (currentPage < 1) currentPage = 1L;
        Long lastPage = pageModel.getLastPage();
        if (currentPage > lastPage && lastPage > 1) currentPage = lastPage;

        pageModel.setCurrentPage(currentPage);
        // 分页数据
        List<BidInfo> bidInfoList = bidInfoService.queryBidInfoPage(loanId,pageModel);

        HashMap<String, Object> map = new HashMap<>();
        map.put("bidInfoList",bidInfoList);
        map.put("pageModel", pageModel);
        map.put("loanId", loanId);
        return map;
    }


}
